<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="800">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入标题"/>
      </el-form-item>
      <el-form-item label="预览图" prop="preImageUrl">
        <UploadImg v-model="formData.preImageUrl" :limit="1" :is-show-tip="false" />
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl">
        <el-input v-model="formData.linkUrl" placeholder="请输入链接地址"/>
      </el-form-item>
      <el-form-item label="标签" prop="tagCode">
        <el-select v-model="formData.tagCode" clearable placeholder="请选择状态" class="!w-240px">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.FAQ_TAG)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="链接方式"  prop="linkType" >
        <el-select v-model="formData.linkType" clearable placeholder="请选择状态" class="!w-240px" @change="changeLinkType">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.INFORMATION_LINK_TYPE)"
            :key="parseInt(dict.value as any)"
            :label="dict.label"
            :value="parseInt(dict.value as any)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" v-if="formData.linkType === 1 || formData.linkType === 3" >
        <label class="el-form-item__label" v-if="formData.linkType === 1 " style="color: red">注意：下面“链接地址”请输入外部网站，例如：https://www.baidu.com</label>
        <label class="el-form-item__label" v-if="formData.linkType === 3 " style="color: red">注意：下面“链接地址”请输入小程序页面地址，例如：pages/index/index</label>
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl" v-if="formData.linkType === 1 || formData.linkType === 3">
        <el-input v-model="formData.linkUrl" placeholder="请输入链接地址"/>
      </el-form-item>
      <el-form-item label="内容" prop="content"  v-if="formData.linkType === 0 || formData.linkType === 2">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="formData.status" clearable placeholder="请选择状态" class="!w-240px">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="parseInt(dict.value as any)"
                :label="dict.label"
                :value="parseInt(dict.value as any)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序号" prop="sort">
            <el-input v-model="formData.sort" placeholder="请输入排序号" class="!w-240px"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions,getStrDictOptions } from '@/utils/dict'
import { InformationApi, InformationVO } from '@/api/zwsj/information'

/** 信息发布 表单 */
defineOptions({ name: 'FaqForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  areaId: undefined,
  type: 9,
  title: undefined,
  preImageUrl: undefined,
  linkType: undefined,
  linkUrl: undefined,
  tagCode: undefined,
  content: undefined,
  status: undefined,
  sort: undefined,
})
const changeLinkType = (val) => {
  if (formType.value === 'create') {
    if (val === 1 || val === 3) {
      formData.value.content = undefined
    }
    if (val === 0 || val === 2) {
      formData.value.linkUrl = undefined
    }
  }
}
const formRules = reactive({
  title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  sort: [{ required: true, message: '排序号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await InformationApi.getInformation(id)
      let content = formData.value.content || ""
      formData.value.content = content
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as InformationApi.InformationVO
    if (formType.value === 'create') {
      await InformationApi.createInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await InformationApi.updateInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    areaId: undefined,
    type: 9,
    title: undefined,
    preImageUrl: undefined,
    linkUrl: undefined,
    tagCode: undefined,
    content: undefined,
    status: undefined,
    sort: undefined,
  }
  formRef.value?.resetFields()
}
</script>
